home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 July / CHIP 2006-07.2.iso / program / web_gelistirme / easyphp1-7_setup.exe / {app} / phpmyadmin / sql.php < prev    next >
Encoding:
PHP Script  |  2003-09-07  |  31.3 KB  |  782 lines

  1. <?php
  2. /* $Id: sql.php,v 1.190 2003/08/06 18:38:04 lem9 Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Gets some core libraries
  7.  */
  8. require('./libraries/grab_globals.lib.php');
  9. require('./libraries/common.lib.php');
  10.  
  11.  
  12. /**
  13.  * Defines the url to return to in case of error in a sql statement
  14.  */
  15. // Security checkings
  16. if (!empty($goto)) {
  17.     $is_gotofile     = ereg_replace('^([^?]+).*$', '\\1', $goto);
  18.     if (!@file_exists('./' . $is_gotofile)) {
  19.         unset($goto);
  20.     } else {
  21.         $is_gotofile = ($is_gotofile == $goto);
  22.     }
  23. } // end if (security checkings)
  24.  
  25. if (empty($goto)) {
  26.     $goto         = (empty($table)) ? $cfg['DefaultTabDatabase'] : $cfg['DefaultTabTable'];
  27.     $is_gotofile  = TRUE;
  28. } // end if
  29. if (!isset($err_url)) {
  30.     $err_url = (!empty($back) ? $back : $goto)
  31.              . '?' . PMA_generate_common_url(isset($db) ? $db : '')
  32.              . ((strpos(' ' . $goto, 'db_details') != 1 && isset($table)) ? '&table=' . urlencode($table) : '');
  33. } // end if
  34.  
  35. // Coming from a bookmark dialog
  36. if (isset($fields['query'])) {
  37.     $sql_query = $fields['query'];
  38. }
  39.  
  40. // This one is just to fill $db
  41. if (isset($fields['dbase'])) {
  42.     $db = $fields['dbase'];
  43. }
  44.  
  45. // Now we can check the parameters
  46. PMA_checkParameters(array('sql_query', 'db'));
  47.  
  48.  
  49. /**
  50.  * Check rights in case of DROP DATABASE
  51.  *
  52.  * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
  53.  * but since a malicious user may pass this variable by url/form, we don't take
  54.  * into account this case.
  55.  */
  56. if (!defined('PMA_CHK_DROP')
  57.     && !$cfg['AllowUserDropDatabase']
  58.     && eregi('DROP[[:space:]]+DATABASE[[:space:]]+', $sql_query)) {
  59.     // Checks if the user is a Superuser
  60.     // TODO: set a global variable with this information
  61.     // loic1: optimized query
  62.     $result = @PMA_mysql_query('USE mysql');
  63.     if (PMA_mysql_error()) {
  64.         include('./header.inc.php');
  65.         PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
  66.     } // end if
  67. } // end if
  68.  
  69.  
  70. /**
  71.  * Bookmark add
  72.  */
  73. if (isset($store_bkm)) {
  74.     include('./libraries/bookmark.lib.php');
  75.     PMA_addBookmarks($fields, $cfg['Bookmark']);
  76.     header('Location: ' . $cfg['PmaAbsoluteUri'] . $goto);
  77. } // end if
  78.  
  79.  
  80. /**
  81.  * Gets the true sql query
  82.  */
  83. // $sql_query has been urlencoded in the confirmation form for drop/delete
  84. // queries or in the navigation bar for browsing among records
  85. if (isset($btnDrop) || isset($navig)) {
  86.     $sql_query = urldecode($sql_query);
  87. }
  88.  
  89. /**
  90.  * Reformat the query
  91.  */
  92.  
  93. $GLOBALS['unparsed_sql'] = $sql_query;
  94. $parsed_sql = PMA_SQP_parse($sql_query);
  95. $analyzed_sql = PMA_SQP_analyze($parsed_sql);
  96. // Bug #641765 - Robbat2 - 12 January 2003, 10:49PM
  97. // Reverted - Robbat2 - 13 January 2003, 2:40PM
  98. $sql_query = PMA_SQP_formatHtml($parsed_sql, 'query_only');
  99.  
  100. // old code did not work, for example, when there is a bracket
  101. // before the SELECT
  102. // so I guess it's ok to check for a real SELECT ... FROM
  103. //$is_select = eregi('^SELECT[[:space:]]+', $sql_query);
  104. $is_select = isset($analyzed_sql[0]['queryflags']['select_from']);
  105.  
  106. // If the query is a Select, extract the db and table names and modify
  107. // $db and $table, to have correct page headers, links and left frame.
  108. // db and table name may be enclosed with backquotes, db is optionnal,
  109. // query may contain aliases.
  110.  
  111. // (TODO: if there are more than one table name in the Select:
  112. // - do not extract the first table name
  113. // - do not show a table name in the page header
  114. // - do not display the sub-pages links)
  115.  
  116. if ($is_select) {
  117.     $prev_db = $db;
  118.     if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name'])) {
  119.         $table = $analyzed_sql[0]['table_ref'][0]['table_true_name'];
  120.     }
  121.     if (isset($analyzed_sql[0]['table_ref'][0]['db'])
  122.        && !empty($analyzed_sql[0]['table_ref'][0]['db'])) {
  123.         $db    = $analyzed_sql[0]['table_ref'][0]['db'];
  124.     }
  125.     else {
  126.         $db = $prev_db;
  127.     }
  128.     $reload  = ($db == $prev_db) ? 0 : 1;
  129. }
  130.  
  131. /**
  132.  * Sets or modifies the $goto variable if required
  133.  */
  134. if ($goto == 'sql.php') {
  135.     $goto = 'sql.php?'
  136.           . PMA_generate_common_url($db, $table)
  137.           . '&pos=' . $pos
  138.           . '&sql_query=' . urlencode($sql_query);
  139. } // end if
  140.  
  141.  
  142. /**
  143.  * Go back to further page if table should not be dropped
  144.  */
  145. if (isset($btnDrop) && $btnDrop == $strNo) {
  146.     if (!empty($back)) {
  147.         $goto = $back;
  148.     }
  149.     if ($is_gotofile) {
  150.         if (strpos(' ' . $goto, 'db_details') == 1 && !empty($table)) {
  151.             unset($table);
  152.         }
  153.         $active_page = $goto;
  154.         include('./' . ereg_replace('\.\.*', '.', $goto));
  155.     } else {
  156.         header('Location: ' . $cfg['PmaAbsoluteUri'] . str_replace('&', '&', $goto));
  157.     }
  158.     exit();
  159. } // end if
  160.  
  161.  
  162. /**
  163.  * Displays the confirm page if required
  164.  *
  165.  * This part of the script is bypassed if $is_js_confirmed = 1 (already checked
  166.  * with js) because possible security issue is not so important here: at most,
  167.  * the confirm message isn't displayed.
  168.  *
  169.  * Also bypassed if only showing php code.or validating a SQL query
  170.  */
  171. if (!$cfg['Confirm']
  172.     || (isset($is_js_confirmed) && $is_js_confirmed)
  173.     || isset($btnDrop)
  174.  
  175.     // if we are coming from a "Create PHP code" or a "Without PHP Code"
  176.     // dialog, we won't execute the query anyway, so don't confirm
  177.     //|| !empty($GLOBALS['show_as_php'])
  178.     || isset($GLOBALS['show_as_php'])
  179.  
  180.     || !empty($GLOBALS['validatequery'])) {
  181.     $do_confirm = FALSE;
  182. } else {
  183.     //$do_confirm = (eregi('DROP[[:space:]]+(IF[[:space:]]+EXISTS[[:space:]]+)?(TABLE|DATABASE[[:space:]])|ALTER[[:space:]]+TABLE[[:space:]]+((`[^`]+`)|([A-Za-z0-9_$]+))[[:space:]]+DROP[[:space:]]|DELETE[[:space:]]+FROM[[:space:]]', $sql_query));
  184.  
  185.     $do_confirm = isset($analyzed_sql[0]['queryflags']['need_confirm']);
  186. }
  187.  
  188. if ($do_confirm) {
  189.     $stripped_sql_query = $sql_query;
  190.     include('./header.inc.php');
  191.     echo $strDoYouReally . ' :<br />' . "\n";
  192.     echo '<tt>' . htmlspecialchars($stripped_sql_query) . '</tt> ?<br/>' . "\n";
  193.     ?>
  194. <form action="sql.php" method="post">
  195.     <?php echo PMA_generate_common_hidden_inputs($db, (isset($table)?$table:'')); ?>
  196.     <input type="hidden" name="sql_query" value="<?php echo urlencode($sql_query); ?>" />
  197.     <input type="hidden" name="zero_rows" value="<?php echo isset($zero_rows) ? $zero_rows : ''; ?>" />
  198.     <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  199.     <input type="hidden" name="back" value="<?php echo isset($back) ? $back : ''; ?>" />
  200.     <input type="hidden" name="reload" value="<?php echo isset($reload) ? $reload : 0; ?>" />
  201.     <input type="hidden" name="purge" value="<?php echo isset($purge) ? $purge : ''; ?>" />
  202.     <input type="hidden" name="cpurge" value="<?php echo isset($cpurge) ? $cpurge : ''; ?>" />
  203.     <input type="hidden" name="purgekey" value="<?php echo isset($purgekey) ? $purgekey : ''; ?>" />
  204.     <input type="hidden" name="show_query" value="<?php echo isset($show_query) ? $show_query : ''; ?>" />
  205.     <input type="submit" name="btnDrop" value="<?php echo $strYes; ?>" />
  206.     <input type="submit" name="btnDrop" value="<?php echo $strNo; ?>" />
  207. </form>
  208.     <?php
  209.     echo "\n";
  210. } // end if
  211.  
  212.  
  213. /**
  214.  * Executes the query and displays results
  215.  */
  216. else {
  217.     if (!isset($sql_query)) {
  218.         $sql_query = '';
  219.     }
  220.     // Defines some variables
  221.     // loic1: A table has to be created -> left frame should be reloaded
  222.     if ((!isset($reload) || $reload == 0)
  223.         && eregi('^CREATE TABLE[[:space:]]+(.*)', $sql_query)) {
  224.         $reload           = 1;
  225.     }
  226.     // Gets the number of rows per page
  227.     if (!isset($session_max_rows)) {
  228.         $session_max_rows = $cfg['MaxRows'];
  229.     } else if ($session_max_rows != 'all') {
  230.         $cfg['MaxRows']   = $session_max_rows;
  231.     }
  232.     // Defines the display mode (horizontal/vertical) and header "frequency"
  233.     if (empty($disp_direction)) {
  234.         $disp_direction   = $cfg['DefaultDisplay'];
  235.     }
  236.     if (empty($repeat_cells)) {
  237.         $repeat_cells     = $cfg['RepeatCells'];
  238.     }
  239.  
  240.     // SK -- Patch: $is_group added for use in calculation of total number of
  241.     //              rows.
  242.     //              $is_count is changed for more correct "LIMIT" clause
  243.     //              appending in queries like
  244.     //                "SELECT COUNT(...) FROM ... GROUP BY ..."
  245.  
  246.     // TODO: detect all this with the parser, to avoid problems finding
  247.     // those strings in comments or backquoted identifiers
  248.  
  249.     $is_explain = $is_count = $is_export = $is_delete = $is_insert = $is_affected = $is_show = $is_maint = $is_analyse = $is_group = $is_func = FALSE;
  250.     if ($is_select) { // see line 141
  251.         $is_group = eregi('(GROUP[[:space:]]+BY|HAVING|SELECT[[:space:]]+DISTINCT)[[:space:]]+', $sql_query);
  252.         $is_func =  !$is_group && (eregi('[[:space:]]+(SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND)\s*\(', $sql_query));
  253.         $is_count = !$is_group && (eregi('^SELECT[[:space:]]+COUNT\((.*\.+)?.*\)', $sql_query));
  254.         $is_export   = (eregi('[[:space:]]+INTO[[:space:]]+OUTFILE[[:space:]]+', $sql_query));
  255.         $is_analyse  = (eregi('[[:space:]]+PROCEDURE[[:space:]]+ANALYSE', $sql_query));
  256.     } else if (eregi('^EXPLAIN[[:space:]]+', $sql_query)) {
  257.         $is_explain  = TRUE;
  258.     } else if (eregi('^DELETE[[:space:]]+', $sql_query)) {
  259.         $is_delete   = TRUE;
  260.         $is_affected = TRUE;
  261.     } else if (eregi('^(INSERT|LOAD[[:space:]]+DATA|REPLACE)[[:space:]]+', $sql_query)) {
  262.         $is_insert   = TRUE;
  263.         $is_affected = TRUE;
  264.     } else if (eregi('^UPDATE[[:space:]]+', $sql_query)) {
  265.         $is_affected = TRUE;
  266.     } else if (eregi('^SHOW[[:space:]]+', $sql_query)) {
  267.         $is_show     = TRUE;
  268.     } else if (eregi('^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+', $sql_query)) {
  269.         $is_maint    = TRUE;
  270.     }
  271.  
  272.     // Do append a "LIMIT" clause?
  273.     if (isset($pos)
  274.         && (!$cfg['ShowAll'] || $session_max_rows != 'all')
  275.         && !($is_count || $is_export || $is_func || $is_analyse)
  276.         && isset($analyzed_sql[0]['queryflags']['select_from'])
  277.         && !eregi('[[:space:]]LIMIT[[:space:]0-9,-]+$', $sql_query)) {
  278.         $sql_limit_to_append = " LIMIT $pos, ".$cfg['MaxRows'];
  279.         if (eregi('(.*)([[:space:]](PROCEDURE[[:space:]](.*)|FOR[[:space:]]+UPDATE|LOCK[[:space:]]+IN[[:space:]]+SHARE[[:space:]]+MODE))$', $sql_query, $regs)) {
  280.             $full_sql_query  = $regs[1] . $sql_limit_to_append . $regs[2];
  281.         } else {
  282.             $full_sql_query  = $sql_query . $sql_limit_to_append;
  283.         }
  284.     } else {
  285.         $full_sql_query      = $sql_query;
  286.     } // end if...else
  287.  
  288.     PMA_mysql_select_db($db);
  289.  
  290.     // If the query is a DELETE query with no WHERE clause, get the number of
  291.     // rows that will be deleted (mysql_affected_rows will always return 0 in
  292.     // this case)
  293.     if ($is_delete
  294.         && eregi('^DELETE([[:space:]].+)?([[:space:]]FROM[[:space:]](.+))$', $sql_query, $parts)
  295.         && !eregi('[[:space:]]WHERE[[:space:]]', $parts[3])) {
  296.         $cnt_all_result = @PMA_mysql_query('SELECT COUNT(*) as count' .  $parts[2]);
  297.         if ($cnt_all_result) {
  298.             $num_rows   = PMA_mysql_result($cnt_all_result, 0, 'count');
  299.             mysql_free_result($cnt_all_result);
  300.         } else {
  301.             $num_rows   = 0;
  302.         }
  303.     }
  304.  
  305.     //  E x e c u t e    t h e    q u e r y
  306.  
  307.     // Only if we didn't ask to see the php code (mikebeck)
  308.     if (isset($GLOBALS['show_as_php']) || !empty($GLOBALS['validatequery'])) {
  309.         unset($result);
  310.         $num_rows = 0;
  311.     }
  312.     else {
  313.         // garvin: Measure query time. TODO-Item http://sourceforge.net/tracker/index.php?func=detail&aid=571934&group_id=23067&atid=377411
  314.         list($usec, $sec) = explode(' ',microtime());
  315.         $querytime_before = ((float)$usec + (float)$sec);
  316.  
  317.         $result   = @PMA_mysql_query($full_sql_query);
  318.  
  319.         list($usec, $sec) = explode(' ',microtime());
  320.         $querytime_after = ((float)$usec + (float)$sec);
  321.  
  322.         $GLOBALS['querytime'] = $querytime_after - $querytime_before;
  323.  
  324.         // Displays an error message if required and stop parsing the script
  325.         if (PMA_mysql_error()) {
  326.             $error        = PMA_mysql_error();
  327.             include('./header.inc.php');
  328.             $full_err_url = (ereg('^(db_details|tbl_properties)', $err_url))
  329.                           ? $err_url . '&show_query=1&sql_query=' . urlencode($sql_query)
  330.                           : $err_url;
  331.             PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
  332.         }
  333.  
  334.         // Gets the number of rows affected/returned
  335.         // (This must be done immediately after the query because
  336.         // mysql_affected_rows() reports about the last query done)
  337.  
  338.         if (!$is_affected) {
  339.             $num_rows = ($result) ? @mysql_num_rows($result) : 0;
  340.         } else if (!isset($num_rows)) {
  341.             $num_rows = @mysql_affected_rows();
  342.         }
  343.  
  344.         // Checks if the current database has changed
  345.         // This could happen if the user sends a query like "USE `database`;"
  346.         $res = PMA_mysql_query('SELECT DATABASE() AS "db";');
  347.         $row = PMA_mysql_fetch_array($res);
  348.         if ($db != $row['db']) {
  349.             $db     = $row['db'];
  350.             $reload = 1;
  351.         }
  352.         @mysql_free_result($res);
  353.         unset($res);
  354.         unset($row);
  355.  
  356.         // tmpfile remove after convert encoding appended by Y.Kawada
  357.         if (function_exists('PMA_kanji_file_conv')
  358.             && (isset($textfile) && file_exists($textfile))) {
  359.             unlink($textfile);
  360.         }
  361.  
  362.         // Counts the total number of rows for the same 'SELECT' query without the
  363.         // 'LIMIT' clause that may have been programatically added
  364.  
  365.         if (empty($sql_limit_to_append)) {
  366.             $unlim_num_rows         = $num_rows;
  367.             // if we did not append a limit, set this to get a correct
  368.             // "Showing rows..." message
  369.             $GLOBALS['session_max_rows'] = 'all';
  370.         }
  371.         else if ($is_select) {
  372.  
  373.                 //    c o u n t    q u e r y
  374.  
  375.                 // If we are "just browsing", there is only one table, 
  376.                 // and no where clause (or just 'WHERE 1 '),
  377.                 // so we do a quick count (which uses MaxExactCount)
  378.                 // because SQL_CALC_FOUND_ROWS
  379.                 // is not quick on large InnoDB tables
  380.  
  381.                 if (!$is_group 
  382.                  && !isset($analyzed_sql[0]['queryflags']['union'])
  383.                  && !isset($analyzed_sql[0]['table_ref'][1]['table_name'])
  384.                  && (empty($analyzed_sql[0]['where_clause'])
  385.                    || $analyzed_sql[0]['where_clause'] == '1 ')) {
  386.  
  387.                     // "j u s t   b r o w s i n g"
  388.                     $unlim_num_rows = PMA_countRecords($db, $table, TRUE);
  389.  
  390.                 } else { // n o t   " j u s t   b r o w s i n g "
  391.  
  392.                     if (PMA_MYSQL_INT_VERSION < 40000) {
  393.                         // TODO: detect DISTINCT in the parser 
  394.                         if (eregi('DISTINCT(.*)', $sql_query)) {
  395.                             $count_what = 'DISTINCT ' . $analyzed_sql[0]['select_expr_clause'];
  396.                         } else {
  397.                             $count_what = '*';
  398.                         }
  399.  
  400.                         $count_query = 'SELECT COUNT(' . $count_what . ') AS count';
  401.                     }
  402.  
  403.                     // add the remaining of select expression if there is
  404.                     // a GROUP BY or HAVING clause
  405.                     if (PMA_MYSQL_INT_VERSION < 40000
  406.                      && $count_what =='*'
  407.                      && (!empty($analyzed_sql[0]['group_by_clause'])
  408.                         || !empty($analyzed_sql[0]['having_clause']))) {
  409.                         $count_query .= ' ,' . $analyzed_sql[0]['select_expr_clause'];
  410.                     }
  411.  
  412.                     if (PMA_MYSQL_INT_VERSION >= 40000) {
  413.                          // add select expression after the SQL_CALC_FOUND_ROWS
  414. //                        if (eregi('DISTINCT(.*)', $sql_query)) {
  415. //                            $count_query .= 'DISTINCT ' . $analyzed_sql[0]['select_expr_clause'];
  416. //                        } else {
  417.                             //$count_query .= $analyzed_sql[0]['select_expr_clause'];
  418.       
  419.                             // for UNION, just adding SQL_CALC_FOUND_ROWS
  420.                             // after the first SELECT works.
  421.  
  422.                             // take the left part, could be:
  423.                             // SELECT
  424.                             // (SELECT
  425.                             $count_query = PMA_SQP_formatHtml($parsed_sql, 'query_only', 0, $analyzed_sql[0]['position_of_first_select'] + 1);
  426.                             $count_query .= ' SQL_CALC_FOUND_ROWS ';
  427.  
  428.                             // add everything that was after the first SELECT
  429.                             $count_query .= PMA_SQP_formatHtml($parsed_sql, 'query_only', $analyzed_sql[0]['position_of_first_select']+1);
  430. //                        }
  431.                     } else { // PMA_MYSQL_INT_VERSION < 40000
  432.  
  433.                         if (!empty($analyzed_sql[0]['from_clause'])) {
  434.                             $count_query .= ' FROM ' . $analyzed_sql[0]['from_clause'];
  435.                         }
  436.                         if (!empty($analyzed_sql[0]['where_clause'])) {
  437.                             $count_query .= ' WHERE ' . $analyzed_sql[0]['where_clause'];
  438.                         }
  439.                         if (!empty($analyzed_sql[0]['group_by_clause'])) {
  440.                             $count_query .= ' GROUP BY ' . $analyzed_sql[0]['group_by_clause'];
  441.                         }
  442.                         if (!empty($analyzed_sql[0]['having_clause'])) {
  443.                             $count_query .= ' HAVING ' . $analyzed_sql[0]['having_clause'];
  444.                         }
  445.                     } // end if
  446.  
  447.                     // if using SQL_CALC_FOUND_ROWS, add a LIMIT to avoid
  448.                     // long delays. Returned count will be complete anyway.
  449.                     // (but a LIMIT would disrupt results in an UNION)
  450.  
  451.                     if (PMA_MYSQL_INT_VERSION >= 40000
  452.                     && !isset($analyzed_sql[0]['queryflags']['union'])) {
  453.                         $count_query .= ' LIMIT 1';
  454.                     }
  455.  
  456.                     // run the count query
  457. //DEBUG echo "trace cq=" . $count_query . "<br/>";
  458.  
  459.                     if (PMA_MYSQL_INT_VERSION < 40000) {
  460.                         if ($cnt_all_result = PMA_mysql_query($count_query)) {
  461.                             if ($is_group && $count_what == '*') {
  462.                                 $unlim_num_rows = @mysql_num_rows($cnt_all_result);
  463.                             } else {
  464.                                 $unlim_num_rows = PMA_mysql_result($cnt_all_result, 0, 'count');
  465.                             }
  466.                             mysql_free_result($cnt_all_result);
  467.                         } else {
  468.                             if (mysql_error()) {
  469.  
  470.                                 // there are some cases where the generated
  471.                                 // count_query (for MySQL 3) is wrong,
  472.                                 // so we get here.
  473.                                 //TODO: use a big unlimited query to get
  474.                                 // the correct number of rows (depending
  475.                                 // on a config variable?)
  476.                                 $unlim_num_rows = 0;
  477.                             }
  478.                         }
  479.                     } else {
  480.                         PMA_mysql_query($count_query);
  481.                         if (mysql_error()) {
  482.                         // void. I tried the case
  483.                         // (SELECT `User`, `Host`, `Db`, `Select_priv` FROM `db`)
  484.                         // UNION (SELECT `User`, `Host`, "%" AS "Db",
  485.                         // `Select_priv`
  486.                         // FROM `user`) ORDER BY `User`, `Host`, `Db`;
  487.                         // and although the generated count_query is wrong
  488.                         // the SELECT FOUND_ROWS() work!
  489.                         }
  490.                         $cnt_all_result = PMA_mysql_query('SELECT FOUND_ROWS() as count');
  491.                         $unlim_num_rows = PMA_mysql_result($cnt_all_result,0,'count');
  492.                     }
  493.             } // end else "just browsing"
  494.  
  495.         } else { // not $is_select
  496.              $unlim_num_rows         = 0;
  497.         } // end rows total count
  498.  
  499.         // garvin: if a table or database gets dropped, check column comments.
  500.         if (isset($purge) && $purge == '1') {
  501.             include('./libraries/relation_cleanup.lib.php');
  502.  
  503.             if (isset($table) && isset($db) && !empty($table) && !empty($db)) {
  504.                 PMA_relationsCleanupTable($db, $table);
  505.             } elseif (isset($db) && !empty($db)) {
  506.                 PMA_relationsCleanupDatabase($db);
  507.             } else {
  508.                 // garvin: VOID. No DB/Table gets deleted.
  509.             } // end if relation-stuff
  510.          } // end if ($purge)
  511.          
  512.         // garvin: If a column gets dropped, do relation magic.
  513.         if (isset($cpurge) && $cpurge == '1' && isset($purgekey)
  514.             && isset($db) && isset($table)
  515.             && !empty($db) && !empty($table) && !empty($purgekey)) {
  516.             include('./libraries/relation_cleanup.lib.php');
  517.             PMA_relationsCleanupColumn($db, $table, $purgekey);
  518.  
  519.         } // end if column PMA_* purge
  520.     } // end else "didn't ask to see php code"
  521.         
  522.  
  523.     // No rows returned -> move back to the calling page
  524.     if ($num_rows < 1 || $is_affected) {
  525.         if ($is_delete) {
  526.             $message = $strDeletedRows . ' ' . $num_rows;
  527.         } else if ($is_insert) {
  528.             $message = $strInsertedRows . ' ' . $num_rows;
  529.             $insert_id = mysql_insert_id();
  530.             if ($insert_id != 0) {
  531.                 $message .= '<br />'.$strInsertedRowId . ' ' . $insert_id;
  532.             }
  533.         } else if ($is_affected) {
  534.             $message = $strAffectedRows . ' ' . $num_rows;
  535.         } else if (!empty($zero_rows)) {
  536.             $message = $zero_rows;
  537.         } else if (!empty($GLOBALS['show_as_php'])) {
  538.             $message = $strPhp;
  539.         } else if (!empty($GLOBALS['validatequery'])) {
  540.             $message = $strValidateSQL;
  541.         } else {
  542.             $message = $strEmptyResultSet;
  543.         }
  544.  
  545.         $message .= ' ' . (isset($GLOBALS['querytime']) ? '(' . sprintf($strQueryTime, $GLOBALS['querytime']) . ')' : '');
  546.  
  547.         if ($is_gotofile) {
  548.             $goto = ereg_replace('\.\.*', '.', $goto);
  549.             // Checks for a valid target script
  550.             if (isset($table) && $table == '') {
  551.                 unset($table);
  552.             }
  553.             if (isset($db) && $db == '') {
  554.                 unset($db);
  555.             }
  556.             $is_db = $is_table = FALSE;
  557.             if (strpos(' ' . $goto, 'tbl_properties') == 1) {
  558.                 if (!isset($table)) {
  559.                     $goto     = 'db_details.php';
  560.                 } else {
  561.                     $is_table = @PMA_mysql_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
  562.                     if (!($is_table && @mysql_numrows($is_table))) {
  563.                         $goto = 'db_details.php';
  564.                         unset($table);
  565.                     }
  566.                 } // end if... else...
  567.             }
  568.             if (strpos(' ' . $goto, 'db_details') == 1) {
  569.                 if (isset($table)) {
  570.                     unset($table);
  571.                 }
  572.                 if (!isset($db)) {
  573.                     $goto     = 'main.php';
  574.                 } else {
  575.                     $is_db    = @PMA_mysql_select_db($db);
  576.                     if (!$is_db) {
  577.                         $goto = 'main.php';
  578.                         unset($db);
  579.                     }
  580.                 } // end if... else...
  581.             }
  582.             // Loads to target script
  583.             if (strpos(' ' . $goto, 'db_details') == 1
  584.                 || strpos(' ' . $goto, 'tbl_properties') == 1) {
  585.                 $js_to_run = 'functions.js';
  586.             }
  587.             if ($goto != 'main.php') {
  588.                 include('./header.inc.php');
  589.             }
  590.             $active_page = $goto;
  591.             include('./' . $goto);
  592.         } // end if file_exist
  593.         else {
  594.             header('Location: ' . $cfg['PmaAbsoluteUri'] . str_replace('&', '&', $goto) . '&message=' . urlencode($message));
  595.         } // end else
  596.         exit();
  597.     } // end no rows returned
  598.  
  599.     // At least one row is returned -> displays a table with results
  600.     else {
  601.         // Displays the headers
  602.         if (isset($show_query)) {
  603.             unset($show_query);
  604.         }
  605.         if (isset($printview) && $printview == '1') {
  606.             include('./header_printview.inc.php');
  607.         } else {
  608.             $js_to_run = 'functions.js';
  609.             unset($message);
  610.             if (!empty($table)) {
  611.                 include('./tbl_properties_common.php');
  612.                 $url_query .= '&goto=tbl_properties.php&back=tbl_properties.php';
  613.                 include('./tbl_properties_table_info.php');
  614.             }
  615.             else {
  616.                 include('./db_details_common.php');
  617.                 include('./db_details_db_info.php');
  618.             }
  619.             include('./libraries/relation.lib.php');
  620.             $cfgRelation = PMA_getRelationsParam();
  621.         }
  622.  
  623.         // Gets the list of fields properties
  624.         if (isset($result) && $result) {
  625.             while ($field = PMA_mysql_fetch_field($result)) {
  626.                 $fields_meta[] = $field;
  627.             }
  628.             $fields_cnt        = count($fields_meta);
  629.         }
  630.  
  631.         // Display previous update query (from tbl_replace)
  632.         if (isset($disp_query) && $cfg['ShowSQL'] == TRUE) {
  633.             $tmp_sql_query = $GLOBALS['sql_query'];
  634.             $tmp_sql_limit_to_append = (isset($GLOBALS['sql_limit_to_append'])?$GLOBALS['sql_limit_to_append']:'');
  635.             $GLOBALS['sql_query'] = $disp_query;
  636.             $GLOBALS['sql_limit_to_append'] = '';
  637.             PMA_showMessage($disp_message);
  638.             $GLOBALS['sql_query'] = $tmp_sql_query;
  639.             $GLOBALS['sql_limit_to_append'] = $tmp_sql_limit_to_append;
  640.         }
  641.  
  642.         // Displays the results in a table
  643.         include('./libraries/display_tbl.lib.php');
  644.         if (empty($disp_mode)) {
  645.             // see the "PMA_setDisplayMode()" function in
  646.             // libraries/display_tbl.lib.php
  647.             $disp_mode = 'urdr111101';
  648.         }
  649.         if (!isset($dontlimitchars)) {
  650.             $dontlimitchars = 0;
  651.         }
  652.  
  653.         PMA_displayTable($result, $disp_mode, $analyzed_sql);
  654.         mysql_free_result($result);
  655.  
  656.         if ($disp_mode[6] == '1' || $disp_mode[9] == '1') {
  657.             echo "\n";
  658.             echo '<p>' . "\n";
  659.  
  660.             // Displays "Insert a new row" link if required
  661.             if ($disp_mode[6] == '1') {
  662.                 $lnk_goto  = 'sql.php?'
  663.                            . PMA_generate_common_url($db, $table)
  664.                            . '&pos=' . $pos
  665.                            . '&session_max_rows=' . $session_max_rows
  666.                            . '&disp_direction=' . $disp_direction
  667.                            . '&repeat_cells=' . $repeat_cells
  668.                            . '&dontlimitchars=' . $dontlimitchars
  669.                            . '&sql_query=' . urlencode($sql_query);
  670.                 $url_query = '?'
  671.                            . PMA_generate_common_url($db, $table)
  672.                            . '&pos=' . $pos
  673.                            . '&session_max_rows=' . $session_max_rows
  674.                            . '&disp_direction=' . $disp_direction
  675.                            . '&repeat_cells=' . $repeat_cells
  676.                            . '&dontlimitchars=' . $dontlimitchars
  677.                            . '&sql_query=' . urlencode($sql_query)
  678.                            . '&goto=' . urlencode($lnk_goto);
  679.  
  680.                 echo '    <!-- Insert a new row -->' . "\n"
  681.                    . '    <a href="tbl_change.php' . $url_query . '">' . $strInsertNewRow . '</a>';
  682.                 if ($disp_mode[9] == '1') {
  683.                     echo '<br />';
  684.                 }
  685.                 echo "\n";
  686.             } // end insert new row
  687.  
  688.             // Displays "printable view" link if required
  689.             if ($disp_mode[9] == '1') {
  690.                 $url_query = '?'
  691.                            . PMA_generate_common_url($db, $table)
  692.                            . '&pos=' . $pos
  693.                            . '&session_max_rows=' . $session_max_rows
  694.                            . '&disp_direction=' . $disp_direction
  695.                            . '&repeat_cells=' . $repeat_cells
  696.                            . '&printview=1'
  697.                            . ((isset($dontlimitchars) && $dontlimitchars == '1') ? '&dontlimitchars=1' : '')
  698.                            . '&sql_query=' . urlencode($sql_query);
  699.                 echo '    <!-- Print view -->' . "\n"
  700.                    . '    <a href="sql.php' . $url_query . '" target="print_view">' . $strPrintView . '</a>' . "\n";
  701.             } // end displays "printable view"
  702.  
  703.             echo '</p>' . "\n";
  704.         }
  705.  
  706.         // Export link, if only one table
  707.         // (the url_query has extra parameters that won't be used to export)
  708.         if (!isset($printview)) {
  709.             if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name']) && !isset($analyzed_sql[0]['table_ref'][1]['table_true_name'])) {
  710.                 $single_table   = '&single_table=true';
  711.             } else {
  712.                 $single_table   = '';
  713.             }
  714.             echo '    <!-- Export -->' . "\n"
  715.                    . '    <a href="tbl_properties_export.php' . $url_query  
  716.                    . '&unlim_num_rows=' . $unlim_num_rows 
  717.                    . $single_table
  718.                    . '">' . $strExport . '</a>' . "\n";
  719.         }
  720.  
  721.         // Bookmark Support if required
  722.         if ($disp_mode[7] == '1'
  723.             && ($cfg['Bookmark']['db'] && $cfg['Bookmark']['table'] && empty($id_bookmark))
  724.             && !empty($sql_query)) {
  725.             echo "\n";
  726.  
  727.             $goto = 'sql.php?'
  728.                   . PMA_generate_common_url($db, $table)
  729.                   . '&pos=' . $pos
  730.                   . '&session_max_rows=' . $session_max_rows
  731.                   . '&disp_direction=' . $disp_direction
  732.                   . '&repeat_cells=' . $repeat_cells
  733.                   . '&dontlimitchars=' . $dontlimitchars
  734.                   . '&sql_query=' . urlencode($sql_query)
  735.                   . '&id_bookmark=1';
  736.             ?>
  737. <!-- Bookmark the query -->
  738. <form action="sql.php" method="post" onsubmit="return emptyFormElements(this, 'fields[label]');">
  739.             <?php
  740.             echo "\n";
  741.             if ($disp_mode[3] == '1') {
  742.                 echo '    <i>' . $strOr . '</i>' . "\n";
  743.             }
  744.             ?>
  745.     <br /><br />
  746.     <?php echo $strBookmarkLabel; ?> :
  747.     <?php echo PMA_generate_common_hidden_inputs(); ?>
  748.     <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  749.     <input type="hidden" name="fields[dbase]" value="<?php echo htmlspecialchars($db); ?>" />
  750.     <input type="hidden" name="fields[user]" value="<?php echo $cfg['Bookmark']['user']; ?>" />
  751.     <input type="hidden" name="fields[query]" value="<?php echo urlencode(isset($complete_query) ? $complete_query : $sql_query); ?>" />
  752.     <input type="text" name="fields[label]" value="" />
  753.     <input type="submit" name="store_bkm" value="<?php echo $strBookmarkThis; ?>" />
  754. </form>
  755.             <?php
  756.         } // end bookmark support
  757.  
  758.         // Do print the page if required
  759.         if (isset($printview) && $printview == '1') {
  760.             echo "\n";
  761.             ?>
  762. <script type="text/javascript" language="javascript1.2">
  763. <!--
  764. // Do print the page
  765. if (typeof(window.print) != 'undefined') {
  766.     window.print();
  767. }
  768. //-->
  769. </script>
  770.             <?php
  771.         } // end print case
  772.     } // end rows returned
  773.  
  774. } // end executes the query
  775. echo "\n\n";
  776.  
  777. /**
  778.  * Displays the footer
  779.  */
  780. require('./footer.inc.php');
  781. ?>
  782.